/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import com.railway.utils.CustomTableModel;
import com.railway.utils.RestServer;
import com.railway.utils.Toast;
import com.toedter.calendar.JDateChooser;
import java.awt.Graphics;
import java.awt.Image;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Kenny
 */
public class StaffStatusPanel extends javax.swing.JPanel {

    Image imgArrow = new ImageIcon(this.getClass().getResource("/com/railway/image/arrow.png")).getImage();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    private ArrayList<String> colName;
    private ArrayList<String> temp;
    private ArrayList<ArrayList<Object>> rowData;
    private CustomTableModel ctmModel;
    private JSONObject json;
    private JSONArray jsonArray;
    private JDateChooser date = new JDateChooser();
    private Object status;
    /**
     * 
     * Creates new form RoutePanel
     */
    public StaffStatusPanel() {
        initComponents();
        initializeTable();
        disableAll();
    }
    
    private void disableAll(){
        jtfID.setEditable(false);
        jtfFirstName.setEditable(false);
        jtfLastName.setEditable(false);
        jtfPosition.setEditable(false);
        jtfYearEmployed.setEditable(false);
        
    }
    


     private void initializeTable() {
        new Thread() {
            @Override
            public void run() {
                colName = new ArrayList<>();
                colName.add("UserID");
                colName.add("FirstName");
                colName.add("LastName");
                colName.add("Position");
                colName.add("YearEmployed");
                colName.add("Status");
                String data = RestServer.httpGet("staffDetail", "");
                try {
                    json = new JSONObject(data);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int status = json.getInt("status");
                        if (status != 404) {
                            rowData = new ArrayList<>();
                            jsonArray = json.getJSONArray("content");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                ArrayList<Object> temp = new ArrayList<>();
                                temp.add(jsonArray.getJSONObject(i).getInt("uid"));
                                temp.add(jsonArray.getJSONObject(i).getString("firstname"));
                                temp.add(jsonArray.getJSONObject(i).getString("lastname"));
                                temp.add(jsonArray.getJSONObject(i).getString("position"));
                                temp.add(jsonArray.getJSONObject(i).getString("yearEmployed"));
                                temp.add(jsonArray.getJSONObject(i).getString("status"));
                                rowData.add(temp);
                            }
                            ctmModel = new CustomTableModel(colName, rowData);
                            jtbStaffStatus.setModel(ctmModel);
                        }
                    } else {
                        Toast.makeText(getParent(), json.getString("message"), Toast.LENGTH_LONG).display();
                    }
                } catch (JSONException ex) {
                }
            }
        }.start();
    }
      
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jpFare = new javax.swing.JPanel();
        jpStaffParticular = new javax.swing.JPanel();
        jlblFirstName = new javax.swing.JLabel();
        jlblLastName = new javax.swing.JLabel();
        jtfLastName = new javax.swing.JTextField();
        jlblPosition = new javax.swing.JLabel();
        jtfID = new javax.swing.JTextField();
        jtfFirstName = new javax.swing.JTextField();
        jlblYearEmployed = new javax.swing.JLabel();
        jtfYearEmployed = new javax.swing.JTextField();
        jtfPosition = new javax.swing.JTextField();
        jlblID = new javax.swing.JLabel();
        jlblStatus = new javax.swing.JLabel();
        jcbStatus = new javax.swing.JComboBox();
        jbtSubmit = new javax.swing.JButton();
        jspTableScroller = new javax.swing.JScrollPane();
        jtbStaffStatus = new javax.swing.JTable();
        jpFareArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), getHeight(), this);
            }
        };

        jLabel1.setText("jLabel1");

        setPreferredSize(new java.awt.Dimension(870, 460));
        setLayout(null);

        jpFare.setBackground(new java.awt.Color(220, 220, 220));
        jpFare.setLayout(null);

        jpStaffParticular.setBorder(javax.swing.BorderFactory.createTitledBorder("Staff Particular"));

        jlblFirstName.setText("First Name");

        jlblLastName.setText("Last Name");

        jlblPosition.setText("Position");

        jlblYearEmployed.setText("Year Employed");

        jlblID.setText("ID");

        jlblStatus.setText("Status");

        jcbStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Available", "Retired", "Deleted" }));

        jbtSubmit.setText("Submit");
        jbtSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSubmitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jpStaffParticularLayout = new javax.swing.GroupLayout(jpStaffParticular);
        jpStaffParticular.setLayout(jpStaffParticularLayout);
        jpStaffParticularLayout.setHorizontalGroup(
            jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpStaffParticularLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jpStaffParticularLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbtSubmit))
                    .addGroup(jpStaffParticularLayout.createSequentialGroup()
                        .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jlblStatus, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jlblYearEmployed, javax.swing.GroupLayout.DEFAULT_SIZE, 81, Short.MAX_VALUE)
                            .addComponent(jlblPosition, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jlblLastName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jlblFirstName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jlblID, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, Short.MAX_VALUE)
                        .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfID, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfFirstName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfLastName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfYearEmployed, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtfPosition, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcbStatus, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(31, Short.MAX_VALUE))
        );
        jpStaffParticularLayout.setVerticalGroup(
            jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpStaffParticularLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlblID))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblFirstName)
                    .addComponent(jtfFirstName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblLastName)
                    .addComponent(jtfLastName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblPosition)
                    .addComponent(jtfPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblYearEmployed)
                    .addComponent(jtfYearEmployed, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jpStaffParticularLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlblStatus)
                    .addComponent(jcbStatus, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jbtSubmit)
                .addContainerGap(173, Short.MAX_VALUE))
        );

        jpFare.add(jpStaffParticular);
        jpStaffParticular.setBounds(10, 10, 320, 420);

        jtbStaffStatus.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jtbStaffStatus.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jtbStaffStatus.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbStaffStatusMouseClicked(evt);
            }
        });
        jspTableScroller.setViewportView(jtbStaffStatus);

        jpFare.add(jspTableScroller);
        jspTableScroller.setBounds(340, 10, 520, 420);

        add(jpFare);
        jpFare.setBounds(0, 20, 870, 440);

        javax.swing.GroupLayout jpFareArrowLayout = new javax.swing.GroupLayout(jpFareArrow);
        jpFareArrow.setLayout(jpFareArrowLayout);
        jpFareArrowLayout.setHorizontalGroup(
            jpFareArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );
        jpFareArrowLayout.setVerticalGroup(
            jpFareArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 20, Short.MAX_VALUE)
        );

        add(jpFareArrow);
        jpFareArrow.setBounds(540, 0, 150, 20);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSubmitActionPerformed
        int selectedRow;
        Object newStatus = null;
        if(jtfID.getText().equals("")){
            Toast.makeText(getParent(), "Please make selection first", Toast.LENGTH_LONG).display();
        }else{
            selectedRow = jtbStaffStatus.getSelectedRow();
            newStatus = jcbStatus.getSelectedItem();
            JOptionPane.showMessageDialog(null, "selectedRow"+ selectedRow + "\nnewStatus" + newStatus);
            JOptionPane.showMessageDialog(null, jcbStatus.getItemCount());
            if(newStatus.equals(jtbStaffStatus.getModel().getValueAt(selectedRow, 5))){
                Toast.makeText(getParent(), "Please select new status to submit", Toast.LENGTH_LONG).display();
            }else{
                Map<String, Object> data = new HashMap<>();
                data.put("status", jcbStatus.getSelectedItem().toString());
                String a = RestServer.httpPut("deleteStaffById", data, jtfID.getText());
                System.out.println(a);
                try {
                    JSONObject json = new JSONObject(a);
                    Toast.makeText(getParent(), "Submitted!", Toast.LENGTH_LONG).display();
                    initializeTable();
                    jtfID.setText(null);
                    jtfFirstName.setText(null);
                    jtfLastName.setText(null);
                    jtfPosition.setText(null);
                    jtfYearEmployed.setText(null);
                    jcbStatus.setSelectedIndex(0);    
                } catch (JSONException ex) {
                    Toast.makeText(getParent(), ""+ex, Toast.LENGTH_LONG).display();
                }
                
            }
        }
    }//GEN-LAST:event_jbtSubmitActionPerformed

    private void jtbStaffStatusMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbStaffStatusMouseClicked
        int selectedRow = jtbStaffStatus.getSelectedRow();
        
        jtfID.setText(""+jtbStaffStatus.getModel().getValueAt(selectedRow, 0));
        jtfFirstName.setText(""+jtbStaffStatus.getModel().getValueAt(selectedRow, 1));
        jtfLastName.setText(""+jtbStaffStatus.getModel().getValueAt(selectedRow, 2));
        jtfPosition.setText(""+jtbStaffStatus.getModel().getValueAt(selectedRow, 3));
        jtfYearEmployed.setText(""+jtbStaffStatus.getModel().getValueAt(selectedRow, 4));
        status = jtbStaffStatus.getModel().getValueAt(selectedRow, 5);
        if(status.equals("Working")){
            jcbStatus.setSelectedIndex(0);
        }else if(status.equals("Retired")){
            jcbStatus.setSelectedIndex(1);
        }else if(status.equals("Deleted")){
            jcbStatus.setSelectedIndex(2);
        }
    }//GEN-LAST:event_jtbStaffStatusMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JButton jbtSubmit;
    private javax.swing.JComboBox jcbStatus;
    private javax.swing.JLabel jlblFirstName;
    private javax.swing.JLabel jlblID;
    private javax.swing.JLabel jlblLastName;
    private javax.swing.JLabel jlblPosition;
    private javax.swing.JLabel jlblStatus;
    private javax.swing.JLabel jlblYearEmployed;
    private javax.swing.JPanel jpFare;
    private javax.swing.JPanel jpFareArrow;
    private javax.swing.JPanel jpStaffParticular;
    private javax.swing.JScrollPane jspTableScroller;
    private javax.swing.JTable jtbStaffStatus;
    private javax.swing.JTextField jtfFirstName;
    private javax.swing.JTextField jtfID;
    private javax.swing.JTextField jtfLastName;
    private javax.swing.JTextField jtfPosition;
    private javax.swing.JTextField jtfYearEmployed;
    // End of variables declaration//GEN-END:variables
}
